/******************************************init.html*************************************************/
function query_data(page) {
	var parm = $('#queryDataForm').serialize() + sys_get_page(page);
	var url = create_url('data-query');
	sys_query_parm = parm;
	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#dataResultList').html(result);
		}
	});
}

//屏蔽
function shield_data(code, name){
	$.Zebra_Dialog('确认要屏蔽资料' + name + ' 吗？',{
		'type': 'question','title': '警告' ,'buttons':[
		  {caption:'屏蔽',callback:function() {
			  var url = create_url('data-shield');
			  aidExecute_data(code, url);
		  }},
		{caption:'取消',callback:function(){}
		}]
	});
}

//解除屏蔽
function unshield_data(code, name){
	$.Zebra_Dialog('确认要解除资料' + name + ' 吗？',{
		'type': 'question','title': '警告' ,'buttons':[
		  {caption:'屏蔽',callback:function() {
			  var url = create_url('data-unshield');
			  aidExecute_data(code, url);
		  }},
		{caption:'取消',callback:function(){}
		}]
	});
}

function aidExecute_data(code, url) {
	var parm = {'code':code};
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				sys_msg('操作成功');
				query_data();
			} else {
				sys_msg('操作失败', '操作失败，请刷新页面再次尝试，如果问题依然存在，请联系管理员!');
			}
		}
	});
}

function addFilesUploadEvent_data() {
	var url = create_url('data-upload');
	var btn = $('#uploadFilesBtn');
	var interval;
	new AjaxUpload(btn, {
		action:url,
		name:'photo',
		multiple: true,
		responseType: 'json',
		onSubmit : function(file, ext) {
			if (ext && /^(pdf|PDF)$/.test(ext)){
				this.setData({
					'info':'文件类型正确'
				});
			} else {
				sys_msg('文件类型错误,只能上传类型为：pdf文件!');
				return false;               
			}
			$('#upload_span').html(sys_loading_img);
			 $("#bar").css("display","");
			 $("#show").html();
			 $("#show").html("<div style='background-color: green; width:1px;height: 15px;' id='jindu'></div>");
			 var par = 1;
             interval = window.setInterval(function(){  
	         	var barWidth = $("#jindu").width();
	         	if(parseInt(barWidth) <= 100 && par <= 100){
	         		$("#jindu").css("width",parseInt(barWidth + 15)+"px");
	         		$("#percent").text(par + "%");
	         		par = par + 15;
	         	}else{
	         		$("#jindu").css("width","170px");
	         		window.clearInterval(interval); 
	         	}
            }, 200);
		},
		onComplete: function(files, response){
			$("#bar").css("display","none");
			if ('error' == response.result){
				sys_msg('失败', '上传文件失败，请检查文件名是否正确!');
			} else if ('file_type_error' == response.result){
				sys_msg('文件类型错误,只能上传类型为：PDF的文件!');
			} else if ('file_size_error' == response.result){
				sys_msg('您上传的单个文件必须小于20M');
			} else {
				var template = $('#tempAnnexListDiv').find('.rep');
				var annexList = $('#annexListTd');
				var index = parseInt($('#tm_annex_index').val());
				$.each(response.list, function(i) {
					var pii = template.clone(true);
					pii.find('.title').html(response.list[i].name).end()
						.find('.name').attr('name', 'list[' + (index) + '].name').val(response.list[i].name).end()
						.find('.size').attr('name', 'list[' + (index) + '].size').val(response.list[i].size).end()
						.find('.sn').attr('name', 'list[' + (index) + '].sn').val(response.list[i].path).end()
						.find('.ext').attr('name', 'list[' + (index) + '].ext').val(response.list[i].ext).end()
						.find('.afcs').val(response.list[i].code).end();
					annexList.append(pii);
					index ++;
					$('#tm_annex_index').val(index);
				});
				sys_msg('上传完毕!');
			}
			this.enable();
		}
	});
}

function save_data(){
	var rows = $("#sskc").find("tr.footable-even");
	if(rows.length < 1){
		sys_msg("请为资料选择所属课程!");
		return;
	}
	var name = $("#name").val();
	if(name == '' || name == null){
		sys_msg("请填写资料名称");
		return;
	}
	var parm = $('#editDataForm').serialize();
	var url = create_url('data-save');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if (result == 'success') {
				sys_msg('操作成功!');
				sys_load_handle('data');
			} else {
				sys_msg('操作失败!');
			}
		}
	});
}

function deleteSelectedItem_data(btn) {
	btn.parents('.rep').remove();
}

//选择课程
function selectCourse_data(){
	var url = create_url('data-aidInit');
	$.colorbox({overlayClose:false, href:url, data:null, close:'[关闭]', width:'850px', height:'650px', open:true});
}

function queryAidCourse_data(page) {
	sys_loading();
	var parm = $('#queryAidCourseForm').serialize() + sys_get_page(page);
	var url = create_url('data-aidList');
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#material_courseResultList').html(result);
		}
	});
}

function choiseCourse_data(obj, code, name){
	var row = $("#tjTable").find("tr.footable-even").clone(true);
	row.find("td:eq(0)").html(name).end()
	.find(".cc").attr({"value":code});
	$("#addTbody").append(row);
	obj.parents("tr.footable-even").remove();
	querycodes_data();
}

function querycodes_data(){
	var rows = $("#addTbody").find("tr.footable-even");
	var codes = '';
	rows.each(function(){
		var cc = $(this).find(".cc").val();
		codes = codes + cc + ",";
	});
	$("#codes").val(codes);
}

function confirmCourse_data(){
	var rows = $("#addTbody").find("tr.footable-even");
	if(rows.length == 0){
		sys_msg("请为资料选择所属课程!");
		return;
	}
	var sskcs = $("#sskc").find("tr.footable-even");
	sskcs.remove();
	var row = $("#sskcTable").find("tr.footable-even");
	rows.each(function(i){
		var cn = $(this).find("td:eq(0)").html();
		var cc = $(this).find(".cc").val();
		var item = row.clone(true);
	     item.find("td:eq(0)").html(cn).end()
		.find(".cc").attr({"name":"cmrList[" + i + "].cc", "value":cc});
		$("#sskc").append(item);
	});
	$.fn.colorbox.close();
}

function deleteChoiseCourse_data(obj){
	obj.parents("tr.footable-even").remove();
	querycodes_data();
	queryAidCourse_data();
}

function deletedCourses_data(obj){
	obj.parents("tr.footable-even").remove();
}

function getCodes_data(){
	var rows = $("#sskc").find("tr.footable-even");
	var codes = '';
	if(rows.length > 0){
		var row = $("#tjTable").find("tr.footable-even");
		rows.each(function(){
			var item = row.clone(true);
			var cc = $(this).find(".cc").val();
			item.find("td:eq(0)").html($(this).find("td:eq(0)").html()).end()
			.find(".cc").val(cc);
			$("#addTbody").append(item);
			codes = codes + cc + ",";
		});
	}
	$("#codes").val(codes);
}